package com.zut.questionnaire.service;

import com.zut.questionnaire.pojo.AnswerSheet;
import com.zut.questionnaire.pojo.Result;
import com.zut.questionnaire.repository.AnswerSheetRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Optional;

@Service
@Transactional
public class AnswerSheetService {

    @Autowired
    private AnswerSheetRepository answerSheetRepository;

    public Result findById(String id){

        Optional<AnswerSheet> optionalAnswerSheet = answerSheetRepository.findById(id);

        AnswerSheet answerSheet=null;

        if(optionalAnswerSheet.isPresent()){
            answerSheet = optionalAnswerSheet.get();

            return Result.OK(answerSheet);
        }
        return Result.ERROR(500,"查询id不存在");
    }

    public Result save(AnswerSheet answerSheet){

        answerSheet.setAnswerDate(LocalDateTime.now());

        answerSheetRepository.save(answerSheet);

        return Result.OK();
    }

    public Result update(String id,AnswerSheet answerSheet){
        answerSheet.setId(id);
        answerSheetRepository.save(answerSheet);

        return Result.OK();
    }

    public Result delete(String id){

        answerSheetRepository.deleteById(id);

        return Result.OK();
    }
}
